package io.itit.ecp.admin.provide.selected;

import io.itit.ecp.admin.provide.action.resp.EnergyConsumptionTypeResp;
import io.itit.ecp.admin.server.entity.EnergyConsumptionTypeEntity;
import io.itit.ecp.admin.server.service.IEnergyConsumptionTypeService;
import io.itit.grass.common.provide.action.resp.SelectedResp;
import io.itit.grass.common.server.selected.ISelected;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;


@RequiredArgsConstructor
@Component(EnergyConsumptionTypeSelected.CODE)
public class EnergyConsumptionTypeSelected implements ISelected<SelectedResp> {

    public static final String CODE = "EnergyConsumptionTypeSelected";

    private final IEnergyConsumptionTypeService energyConsumptionTypeService;

    @Override
    public String code() {
        return CODE;
    }

    @Override
    public List<SelectedResp> get(String param) {
        return energyConsumptionTypeService.lambdaQuery()
                .eq(EnergyConsumptionTypeEntity::getStatus, "1")
                .orderByAsc(EnergyConsumptionTypeEntity::getCreateTm)
                .list()
                .stream()
                .map(t -> SelectedResp.builder()
                        .label(t.getEnergyName())
                        .value(t.getId())
                        .baseObj(new EnergyConsumptionTypeResp()
                                .setEnergyUnit(t.getEnergyUnit()))
                        .build()
                )
                .collect(Collectors.toList());
    }
}
